package logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.DataConnection;


/** UIConfig class.
 * @author Jesus
 *
 */
public class UIConfig {
	
	/** Writes or overwrites all the parameters into the UICONFIG table.
	 * @param userName : The current logged user's nick name.
	 * @param fullScreen : A Boolean with true if the full screen is enabled or false otherwise.
	 * @param resolution : Current Screen Resolution.
	 * @param decoration : A Boolean with a true if the decoration is enabled or false otherwise.
	 */
	public static void write(String userName, Boolean fullScreen, String resolution, Boolean decoration) {
		Integer intScreen = fullScreen ? 1 : 0;
		Integer intDecoration = decoration ? 1 : 0;
		try {
			Connection connection = database.DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select * from UICONFIG where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resultSet = prepStatement.executeQuery();
			if(resultSet.next()) {
				resultSet.close();
				prepStatement.close();
				//Overwrites the user UIConfig.
				prepStatement = connection.prepareStatement("Update UICONFIG set isOnFullScreen = ?, resolution = ?, isDecorated = ?" +
						" where nick = ?");
				prepStatement.setString(1, intScreen.toString());
				prepStatement.setString(2, resolution);
				prepStatement.setString(3, intDecoration.toString());
				prepStatement.setString(4, userName);
			} else {
				resultSet.close();
				prepStatement.close();
				//Creates new user UIConfig.
				prepStatement = connection.prepareStatement("Insert into UICONFIG values(?, ?, ?, ?);");
				prepStatement.setString(1, userName);
				prepStatement.setString(2, intScreen.toString());
				prepStatement.setString(3, resolution);
				prepStatement.setString(4, intDecoration.toString());
			}
			prepStatement.executeUpdate();
			prepStatement.close();
			connection.close();
		}catch(SQLException e) {
			
		}
	}
	
	/**Returns the UIConfiguration for the current logged user.
	 * @param userName : The current logged user's nick name.
	 * @return a list of object with a full screen boolean, a resolution string and a decorated boolean.
	 * @throws NoSaveException if the select of the logged user returns a closed result set.
	 */
	public static ArrayList<Object> load(String userName) throws NoSaveException {
		ArrayList<Object> config = new ArrayList<Object>();
		try {
			Connection connection = DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select isOnFullScreen, resolution, isDecorated from UICONFIG" +
					" where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resulSet = prepStatement.executeQuery();
			if(resulSet.next()) {
				config.add(Utilities.intToBoolean(resulSet.getInt("isOnFullScreen")));
				config.add(resulSet.getString("resolution"));
				config.add(Utilities.intToBoolean(resulSet.getInt("isDecorated")));
				resulSet.close();
				prepStatement.close();
				connection.close();
			} else {
				resulSet.close();
				prepStatement.close();
				connection.close();
				throw new NoSaveException();
			}
		} catch (SQLException e) {
		
		}
		return config;
	}
}